home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC-2.3.3r12 / Sources-CPlus / cp-cvt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-18  |  62.9 KB  |  2,120 lines  |  [TEXT/MPS ]

  1. /* Language-level data type conversion for GNU C++.
  2.    Copyright (C) 1987, 1988, 1992 Free Software Foundation, Inc.
  3.    Hacked by Michael Tiemann (tiemann@cygnus.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* This file contains the functions for converting C expressions
  23.    to different data types.  The only entry point is `convert'.
  24.    Every language front end must have a `convert' function
  25.    but what kind of conversions it does will depend on the language.  */
  26.  
  27. #include "config.h"
  28. #include "tree.h"
  29. #include "flags.h"
  30. #include "cp-tree.h"
  31. #include "cp-class.h"
  32.  
  33. #define NULL 0
  34.  
  35. extern void warn_for_assignment ();
  36.  
  37. /* Change of width--truncation and extension of integers or reals--
  38.    is represented with NOP_EXPR.  Proper functioning of many things
  39.    assumes that no other conversions can be NOP_EXPRs.
  40.  
  41.    Conversion between integer and pointer is represented with CONVERT_EXPR.
  42.    Converting integer to real uses FLOAT_EXPR
  43.    and real to integer uses FIX_TRUNC_EXPR.
  44.  
  45.    Here is a list of all the functions that assume that widening and
  46.    narrowing is always done with a NOP_EXPR:
  47.      In c-convert.c, convert_to_integer.
  48.      In c-typeck.c, build_binary_op_nodefault (boolean ops),
  49.         and truthvalue_conversion.
  50.      In expr.c: expand_expr, for operands of a MULT_EXPR.
  51.      In fold-const.c: fold.
  52.      In tree.c: get_narrower and get_unwidened.
  53.  
  54.    C++: in multiple-inheritance, converting between pointers may involve
  55.    adjusting them by a delta stored within the class definition.  */
  56.  
  57. /* Subroutines of `convert'.  */
  58.  
  59. static tree
  60. convert_to_pointer (type, expr)
  61.      tree type, expr;
  62. {
  63.   register tree intype = TREE_TYPE (expr);
  64.   register enum tree_code form = TREE_CODE (intype);
  65.   
  66.   if (integer_zerop (expr))
  67.     {
  68.       if (type == TREE_TYPE (null_pointer_node))
  69.     return null_pointer_node;
  70.       expr = build_int_2 (0, 0);
  71.       TREE_TYPE (expr) = type;
  72.       return expr;
  73.     }
  74.  
  75.   if (form == POINTER_TYPE)
  76.     {
  77.       intype = TYPE_MAIN_VARIANT (intype);
  78.  
  79.       if (TYPE_MAIN_VARIANT (type) != intype
  80.       && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
  81.       && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
  82.     {
  83.       enum tree_code code = PLUS_EXPR;
  84.       tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
  85.       if (binfo == error_mark_node)
  86.         return error_mark_node;
  87.       if (binfo == NULL_TREE)
  88.         {
  89.           binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
  90.           if (binfo == error_mark_node)
  91.         return error_mark_node;
  92.           code = MINUS_EXPR;
  93.         }
  94.       if (binfo)
  95.         {
  96.           if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
  97.           || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
  98.           || ! BINFO_OFFSET_ZEROP (binfo))
  99.         {
  100.           /* Need to get the path we took.  */
  101.           tree path;
  102.  
  103.           if (code == PLUS_EXPR)
  104.             get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
  105.           else
  106.             get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
  107.           return build_vbase_path (code, type, expr, path, 0);
  108.         }
  109.         }
  110.     }
  111.       return build1 (NOP_EXPR, type, expr);
  112.     }
  113.  
  114.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  115.     {
  116.       if (type_precision (intype) == POINTER_SIZE)
  117.     return build1 (CONVERT_EXPR, type, expr);
  118.       return convert_to_pointer (type,
  119.                  convert (type_for_size (POINTER_SIZE, 0),
  120.                       expr));
  121.     }
  122.  
  123.   my_friendly_assert (form != OFFSET_TYPE, 186);
  124.  
  125.   if (IS_AGGR_TYPE (intype))
  126.     {
  127.       /* If we cannot convert to the specific pointer type,
  128.      try to convert to the type `void *'.  */
  129.       tree rval;
  130.       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  131.       if (rval)
  132.     {
  133.       if (rval == error_mark_node)
  134.         error ("ambiguous pointer conversion");
  135.       return rval;
  136.     }
  137.     }
  138.  
  139.   error ("cannot convert to a pointer type");
  140.  
  141.   return null_pointer_node;
  142. }
  143.  
  144. /* Like convert, except permit conversions to take place which
  145.    are not normally allowed due to visibility restrictions
  146.    (such as conversion from sub-type to private super-type).  */
  147. static tree
  148. convert_to_pointer_force (type, expr)
  149.      tree type, expr;
  150. {
  151.   register tree intype = TREE_TYPE (expr);
  152.   register enum tree_code form = TREE_CODE (intype);
  153.   
  154.   if (integer_zerop (expr))
  155.     {
  156.       if (type == TREE_TYPE (null_pointer_node))
  157.     return null_pointer_node;
  158.       expr = build_int_2 (0, 0);
  159.       TREE_TYPE (expr) = type;
  160.       return expr;
  161.     }
  162.  
  163.   if (form == POINTER_TYPE)
  164.     {
  165.       intype = TYPE_MAIN_VARIANT (intype);
  166.  
  167.       if (TYPE_MAIN_VARIANT (type) != intype
  168.       && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
  169.       && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
  170.     {
  171.       enum tree_code code = PLUS_EXPR;
  172.       tree path;
  173.       int distance = get_base_distance (TREE_TYPE (type),
  174.                         TREE_TYPE (intype), 0, &path);
  175.       if (distance == -2)
  176.         {
  177.         ambig:
  178.           error_with_aggr_type (TREE_TYPE (type), "type `%s' is ambiguous baseclass of `%s'",
  179.                     TYPE_NAME_STRING (TREE_TYPE (intype)));
  180.           return error_mark_node;
  181.         }
  182.       if (distance == -1)
  183.         {
  184.           distance = get_base_distance (TREE_TYPE (intype),
  185.                         TREE_TYPE (type), 0, &path);
  186.           if (distance == -2)
  187.         goto ambig;
  188.           if (distance < 0)
  189.         /* Doesn't need any special help from us.  */
  190.         return build1 (NOP_EXPR, type, expr);
  191.  
  192.           code = MINUS_EXPR;
  193.         }
  194.       return build_vbase_path (code, type, expr, path, 0);
  195.     }
  196.       return build1 (NOP_EXPR, type, expr);
  197.     }
  198.  
  199.   return convert_to_pointer (type, expr);
  200. }
  201.  
  202. /* We are passing something to a function which requires a reference.
  203.    The type we are interested in is in TYPE. The initial
  204.    value we have to begin with is in ARG.
  205.  
  206.    FLAGS controls how we manage visibility checking.
  207.    CHECKCONST controls if we report error messages on const subversion.  */
  208. static tree
  209. build_up_reference (type, arg, flags, checkconst)
  210.      tree type, arg;
  211.      int flags, checkconst;
  212. {
  213.   tree rval, targ;
  214.   int literal_flag = 0;
  215.   tree argtype = TREE_TYPE (arg), basetype = argtype;
  216.   tree target_type = TREE_TYPE (type);
  217.   tree binfo = NULL_TREE;
  218.  
  219.   my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
  220.   if (flags != 0
  221.       && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
  222.       && IS_AGGR_TYPE (argtype)
  223.       && IS_AGGR_TYPE (target_type))
  224.     {
  225.       binfo = get_binfo (target_type, argtype,
  226.                   (flags & LOOKUP_PROTECTED_OK) ? 3 : 2);
  227.       if ((flags & LOOKUP_PROTECT) && binfo == error_mark_node)
  228.     return error_mark_node;
  229.       if (basetype == NULL_TREE)
  230.     return error_not_base_type (target_type, argtype);
  231.       basetype = BINFO_TYPE (binfo);
  232.     }
  233.  
  234.   /* Pass along const and volatile down into the type. */
  235.   if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
  236.     target_type = build_type_variant (target_type, TYPE_READONLY (type),
  237.                       TYPE_VOLATILE (type));
  238.   targ = arg;
  239.   if (TREE_CODE (targ) == SAVE_EXPR)
  240.     targ = TREE_OPERAND (targ, 0);
  241.  
  242.   switch (TREE_CODE (targ))
  243.     {
  244.     case INDIRECT_REF:
  245.       /* This is a call to a constructor which did not know what it was
  246.      initializing until now: it needs to initialize a temporary.  */
  247.       if (TREE_HAS_CONSTRUCTOR (targ))
  248.     {
  249.       tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0), 1);
  250.       TREE_HAS_CONSTRUCTOR (targ) = 0;
  251.       return build_up_reference (type, temp, flags, 1);
  252.     }
  253.       /* Let &* cancel out to simplify resulting code.
  254.          Also, throw away intervening NOP_EXPRs.  */
  255.       arg = TREE_OPERAND (targ, 0);
  256.       if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR
  257.       || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg)))
  258.     arg = TREE_OPERAND (arg, 0);
  259.  
  260.       /* in doing a &*, we have to get rid of the const'ness on the pointer
  261.      value.  Haven't thought about volatile here.  Pointers come to mind
  262.      here.  */
  263.       if (TREE_READONLY (arg))
  264.     {
  265.       arg = copy_node (arg);
  266.       TREE_READONLY (arg) = 0;
  267.     }
  268.  
  269.       rval = build1 (CONVERT_EXPR, type, arg);
  270.       TREE_REFERENCE_EXPR (rval) = 1;
  271.  
  272.       /* propagate the const flag on something like:
  273.  
  274.      class Base {
  275.      public:
  276.        int foo;
  277.      };
  278.  
  279.       class Derived : public Base {
  280.       public:
  281.     int bar;
  282.       };
  283.  
  284.       void func(Base&);
  285.  
  286.       void func2(const Derived& d) {
  287.     func(d);
  288.       }
  289.  
  290.         on the d parameter.  The below could have been avoided, if the flags
  291.         were down in the tree, not sure why they are not.  (mrs) */
  292.       /* The below code may have to be propagated to other parts of this
  293.      switch.  */
  294.       if (TREE_READONLY (targ) && !TREE_READONLY (arg)
  295.       && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL)
  296.       && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
  297.       && (TYPE_READONLY (target_type) && checkconst))
  298.     {
  299.       arg = copy_node (arg);
  300.       TREE_READONLY (arg) = TREE_READONLY (targ);
  301.     }
  302.       literal_flag = TREE_CONSTANT (arg);
  303.  
  304.       goto done_but_maybe_warn;
  305.  
  306.       /* Get this out of a register if we happened to be in one by accident.
  307.      Also, build up references to non-lvalues it we must.  */
  308.       /* For &x[y], return (&) x+y */
  309.     case ARRAY_REF:
  310.       if (mark_addressable (TREE_OPERAND (targ, 0)) == 0)
  311.     return error_mark_node;
  312.       rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0),
  313.                   TREE_OPERAND (targ, 1));
  314.       TREE_TYPE (rval) = type;
  315.       if (TREE_CONSTANT (TREE_OPERAND (targ, 1))
  316.       && staticp (TREE_OPERAND (targ, 0)))
  317.     TREE_CONSTANT (rval) = 1;
  318.       goto done;
  319.  
  320.     case SCOPE_REF:
  321.       /* Could be a reference to a static member.  */
  322.       {
  323.     tree field = TREE_OPERAND (targ, 1);
  324.     if (TREE_STATIC (field))
  325.       {
  326.         rval = build1 (ADDR_EXPR, type, field);
  327.         literal_flag = 1;
  328.         goto done;
  329.       }
  330.       }
  331.       /* we should have farmed out member pointers above.  */
  332.       my_friendly_assert (0, 188);
  333.  
  334.     case COMPONENT_REF:
  335.       rval = build_component_addr (targ, build_pointer_type (argtype),
  336.                    "attempt to make a reference to bit-field structure member `%s'");
  337.       TREE_TYPE (rval) = type;
  338.       literal_flag = staticp (TREE_OPERAND (targ, 0));
  339.  
  340.       goto done_but_maybe_warn;
  341.  
  342.       /* Anything not already handled and not a true memory reference
  343.      needs to have a reference built up.  Do so silently for
  344.      things like integers and return values from function,
  345.      but complain if we need a reference to something declared
  346.      as `register'.  */
  347.  
  348.     case RESULT_DECL:
  349.       if (staticp (targ))
  350.     literal_flag = 1;
  351.       TREE_ADDRESSABLE (targ) = 1;
  352.       put_var_into_stack (targ);
  353.       break;
  354.  
  355.     case PARM_DECL:
  356.       if (targ == current_class_decl)
  357.     {
  358.       error ("address of `this' not available");
  359. #if 0
  360.       /* This code makes the following core dump the compiler on a sun4,
  361.          if the code below is used.
  362.  
  363.          class e_decl;
  364.          class a_decl;
  365.          typedef a_decl* a_ref;
  366.  
  367.          class a_s {
  368.          public:
  369.            a_s();
  370.            void* append(a_ref& item);
  371.          };
  372.          class a_decl {
  373.          public:
  374.            a_decl (e_decl *parent);
  375.            a_s  generic_s;
  376.            a_s  decls;
  377.            e_decl* parent;
  378.          };
  379.  
  380.          class e_decl {
  381.          public:
  382.            e_decl();
  383.            a_s implementations;
  384.          };
  385.  
  386.          void foobar(void *);
  387.  
  388.          a_decl::a_decl(e_decl *parent) {
  389.            parent->implementations.append(this);
  390.          }
  391.        */
  392.  
  393.       TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */
  394.       put_var_into_stack (targ);
  395. #else
  396.       return error_mark_node;
  397. #endif
  398.       break;
  399.     }
  400.       /* Fall through.  */
  401.     case VAR_DECL:
  402.     case CONST_DECL:
  403.       if (DECL_REGISTER (targ) && !TREE_ADDRESSABLE (targ))
  404.     warning ("address needed to build reference for `%s', which is declared `register'",
  405.          IDENTIFIER_POINTER (DECL_NAME (targ)));
  406.       else if (staticp (targ))
  407.     literal_flag = 1;
  408.  
  409.       TREE_ADDRESSABLE (targ) = 1;
  410.       put_var_into_stack (targ);
  411.       break;
  412.  
  413.     case COMPOUND_EXPR:
  414.       {
  415.     tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
  416.                           LOOKUP_PROTECT, checkconst);
  417.     rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
  418.     TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1));
  419.     return rval;
  420.       }
  421.  
  422.     case MODIFY_EXPR:
  423.     case INIT_EXPR:
  424.       {
  425.     tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
  426.                           LOOKUP_PROTECT, checkconst);
  427.     rval = build (COMPOUND_EXPR, type, arg, real_reference);
  428.     TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0));
  429.     return rval;
  430.       }
  431.  
  432.     case COND_EXPR:
  433.       return build (COND_EXPR, type,
  434.             TREE_OPERAND (targ, 0),
  435.             build_up_reference (type, TREE_OPERAND (targ, 1),
  436.                     LOOKUP_PROTECT, checkconst),
  437.             build_up_reference (type, TREE_OPERAND (targ, 2),
  438.                     LOOKUP_PROTECT, checkconst));
  439.  
  440.     case WITH_CLEANUP_EXPR:
  441.       return build (WITH_CLEANUP_EXPR, type,
  442.             build_up_reference (type, TREE_OPERAND (targ, 0),
  443.                     LOOKUP_PROTECT, checkconst),
  444.             0, TREE_OPERAND (targ, 2));
  445.  
  446.     case BIND_EXPR:
  447.       arg = TREE_OPERAND (targ, 1);
  448.       if (arg == NULL_TREE)
  449.     {
  450.       compiler_error ("({ ... }) expression not expanded when needed for reference");
  451.       return error_mark_node;
  452.     }
  453.       rval = build1 (ADDR_EXPR, type, arg);
  454.       TREE_REFERENCE_EXPR (rval) = 1;
  455.       return rval;
  456.  
  457.     default:
  458.       break;
  459.     }
  460.  
  461.   if (TREE_ADDRESSABLE (targ) == 0)
  462.     {
  463.       tree temp;
  464.  
  465.       if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype))
  466.     {
  467.       temp = build_cplus_new (argtype, targ, 1);
  468.       rval = build1 (ADDR_EXPR, type, temp);
  469.       goto done;
  470.     }
  471.       else
  472.     {
  473.       temp = get_temp_name (argtype, 0);
  474.       if (global_bindings_p ())
  475.         {
  476.           /* Give this new temp some rtl and initialize it.  */
  477.           DECL_INITIAL (temp) = targ;
  478.           TREE_STATIC (temp) = 1;
  479.           finish_decl (temp, targ, NULL_TREE, 0);
  480.           /* Do this after declaring it static.  */
  481.           rval = build_unary_op (ADDR_EXPR, temp, 0);
  482.           literal_flag = TREE_CONSTANT (rval);
  483.           goto done;
  484.         }
  485.       else
  486.         {
  487.           rval = build_unary_op (ADDR_EXPR, temp, 0);
  488.           /* Put a value into the rtl.  */
  489.           if (IS_AGGR_TYPE (argtype))
  490.         {
  491.           /* This may produce surprising results,
  492.              since we commit to initializing the temp
  493.              when the temp may not actually get used.  */
  494.           expand_aggr_init (temp, targ, 0);
  495.           TREE_TYPE (rval) = type;
  496.           literal_flag = TREE_CONSTANT (rval);
  497.           goto done;
  498.         }
  499.           else
  500.         {
  501.           if (binfo && !BINFO_OFFSET_ZEROP (binfo))
  502.             rval = convert_pointer_to (target_type, rval);
  503.           else
  504.             TREE_TYPE (rval) = type;
  505.           return build (COMPOUND_EXPR, type,
  506.                 build (MODIFY_EXPR, argtype, temp, arg), rval);
  507.         }
  508.         }
  509.     }
  510.     }
  511.   else
  512.     {
  513.       if (TREE_CODE (arg) == SAVE_EXPR)
  514.     my_friendly_abort (5);
  515.       rval = build1 (ADDR_EXPR, type, arg);
  516.     }
  517.  
  518.  done_but_maybe_warn:
  519.   if (checkconst && TREE_READONLY (arg) && ! TYPE_READONLY (target_type))
  520.     readonly_warning_or_error (arg, "conversion to reference");
  521.  
  522.  done:
  523.   if (TYPE_USES_COMPLEX_INHERITANCE (argtype))
  524.     {
  525.       TREE_TYPE (rval) = TYPE_POINTER_TO (argtype);
  526.       rval = convert_pointer_to (target_type, rval);
  527.       TREE_TYPE (rval) = type;
  528.     }
  529.   TREE_CONSTANT (rval) = literal_flag;
  530.   return rval;
  531. }
  532.  
  533. #ifdef MPW
  534. #pragma segment CPCVT01
  535. #endif
  536.  
  537. /* For C++: Only need to do one-level references, but cannot
  538.    get tripped up on signed/unsigned differences.
  539.  
  540.    If DECL is NULL_TREE it means convert as though casting (by force).
  541.    If it is ERROR_MARK_NODE, it means the conversion is implicit,
  542.    and that temporaries may be created.
  543.    Make sure the use of user-defined conversion operators is un-ambiguous.
  544.    Otherwise, DECL is a _DECL node which can be used in error reporting.
  545.  
  546.    FNDECL, PARMNUM, and ERRTYPE are only used when checking for use of
  547.    volatile or const references where they aren't desired.  */
  548. tree
  549. convert_to_reference (decl, reftype, expr, fndecl, parmnum,
  550.               errtype, strict, flags)
  551.      tree decl;
  552.      tree reftype, expr;
  553.      tree fndecl;
  554.      int parmnum;
  555.      char *errtype;
  556.      int strict, flags;
  557. {
  558.   register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
  559.   register tree intype = TREE_TYPE (expr);
  560.   register enum tree_code form = TREE_CODE (intype);
  561.   tree rval = NULL_TREE;
  562.  
  563.   if (form == REFERENCE_TYPE)
  564.     intype = TREE_TYPE (intype);
  565.   intype = TYPE_MAIN_VARIANT (intype);
  566.  
  567.   /* @@ Probably need to have a check for X(X&) here.  */
  568.  
  569.   if (IS_AGGR_TYPE (intype))
  570.     {
  571.       rval = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
  572.       if (rval)
  573.     {
  574.       if (rval == error_mark_node)
  575.         error ("ambiguous pointer conversion");
  576.       return rval;
  577.     }
  578.       else if (type != intype
  579.            && (rval = build_type_conversion (CONVERT_EXPR, type, expr, 1)))
  580.     {
  581.       if (rval == error_mark_node)
  582.         return rval;
  583.       if (TYPE_NEEDS_DESTRUCTOR (type))
  584.         {
  585.           rval = convert_to_reference (NULL_TREE, reftype, rval, NULL_TREE, -1, (char *)NULL, strict, flags);
  586.         }
  587.       else
  588.         {
  589.           decl = get_temp_name (type, 0);
  590.           rval = build (INIT_EXPR, type, decl, rval);
  591.           rval = build (COMPOUND_EXPR, reftype, rval,
  592.                 convert_to_reference (NULL_TREE, reftype, decl,
  593.                           NULL_TREE, -1, (char *)NULL,
  594.                           strict, flags));
  595.         }
  596.     }
  597.  
  598.       if (form == REFERENCE_TYPE
  599.       && type != intype
  600.       && TYPE_USES_COMPLEX_INHERITANCE (intype))
  601.     {
  602.       /* If it may move around, build a fresh reference.  */
  603.       expr = convert_from_reference (expr);
  604.       form = TREE_CODE (TREE_TYPE (expr));
  605.     }
  606.     }
  607.  
  608.   /* @@ Perhaps this should try to go through a constructor first
  609.      @@ for proper initialization, but I am not sure when that
  610.      @@ is needed or desirable.
  611.  
  612.      @@ The second disjunct is provided to make references behave
  613.      @@ as some people think they should, i.e., an interconvertability
  614.      @@ between references to builtin types (such as short and
  615.      @@ unsigned short).  There should be no conversion between
  616.      @@ types whose codes are different, or whose sizes are different.  */
  617.  
  618.   if (((IS_AGGR_TYPE (type) || IS_AGGR_TYPE (intype))
  619.        && comptypes (type, intype, strict))
  620.       || (!IS_AGGR_TYPE (type)
  621.       && TREE_CODE (type) == TREE_CODE (intype)
  622.       && int_size_in_bytes (type) == int_size_in_bytes (intype)))
  623.     {
  624.       /* Section 13.  */
  625.       /* Since convert_for_initialization didn't call convert_for_assignment,
  626.      we have to do this checking here.  XXX We should have a common
  627.      routine between here and convert_for_assignment.  */
  628.       if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
  629.     {
  630.       register tree ttl = TREE_TYPE (reftype);
  631.       register tree ttr = TREE_TYPE (TREE_TYPE (expr));
  632.  
  633.       if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
  634.         warn_for_assignment ("%s of non-`const &' reference from `const &'",
  635.                  "reference to const given for argument %d of `%s'",
  636.                  errtype, fndecl, parmnum, pedantic);
  637.       if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
  638.         warn_for_assignment ("%s of non-`volatile &' reference from `volatile &'",
  639.                  "reference to volatile given for argument %d of `%s'",
  640.                  errtype, fndecl, parmnum, pedantic);
  641.     }
  642.  
  643.       /* If EXPR is of aggregate type, and is really a CALL_EXPR,
  644.      then we don't need to convert it to reference type if
  645.      it is only being used to initialize DECL which is also
  646.      of the same aggregate type.  */
  647.       if (form == REFERENCE_TYPE
  648.       || (decl != NULL_TREE && decl != error_mark_node
  649.           && IS_AGGR_TYPE (type)
  650.           && TREE_CODE (expr) == CALL_EXPR
  651.           && TYPE_MAIN_VARIANT (type) == intype))
  652.     {
  653.       if (decl && decl != error_mark_node)
  654.         {
  655.           tree e1 = build (INIT_EXPR, void_type_node, decl, expr);
  656.           tree e2;
  657.  
  658.           TREE_SIDE_EFFECTS (e1) = 1;
  659.           if (form == REFERENCE_TYPE)
  660.         e2 = build1 (NOP_EXPR, reftype, decl);
  661.           else
  662.         {
  663.           e2 = build_unary_op (ADDR_EXPR, decl, 0);
  664.           TREE_TYPE (e2) = reftype;
  665.           TREE_REFERENCE_EXPR (e2) = 1;
  666.         }
  667.           return build_compound_expr (tree_cons (NULL_TREE, e1,
  668.                              build_tree_list (NULL_TREE, e2)));
  669.         }
  670.       expr = copy_node (expr);
  671.       TREE_TYPE (expr) = reftype;
  672.       return expr;
  673.     }
  674.       if (decl == error_mark_node)
  675.     flags |= LOOKUP_PROTECTED_OK;
  676.       return build_up_reference (reftype, expr, flags, decl!=NULL_TREE);
  677.     }
  678.  
  679.   /* Definitely need to go through a constructor here.  */
  680.   if (TYPE_HAS_CONSTRUCTOR (type))
  681.     {
  682.       tree init = build_method_call (NULL_TREE, constructor_name (type),
  683.                      build_tree_list (NULL_TREE, expr),
  684.                      TYPE_BINFO (type), LOOKUP_NO_CONVERSION);
  685.       tree rval1;
  686.  
  687.       if (init != error_mark_node)
  688.     if (rval)
  689.       {
  690.         error ("both constructor and type conversion operator apply");
  691.         return error_mark_node;
  692.       }
  693.  
  694.       init = build_method_call (NULL_TREE, constructor_name (type),
  695.                 build_tree_list (NULL_TREE, expr),
  696.                 TYPE_BINFO (type), LOOKUP_NORMAL);
  697.  
  698.       if (init == error_mark_node)
  699.     return error_mark_node;
  700.       rval = build_cplus_new (type, init, 1);
  701.       if (decl == error_mark_node)
  702.     flags |= LOOKUP_PROTECTED_OK;
  703.       return build_up_reference (reftype, rval, flags, decl!=NULL_TREE);
  704.     }
  705.  
  706.   if (rval)
  707.     {
  708.       /* If we found a way to convert earlier, then use it. */
  709.       return rval;
  710.     }
  711.  
  712.   my_friendly_assert (form != OFFSET_TYPE, 189);
  713.  
  714.   /* This is in two pieces for now, because pointer to first becomes
  715.      invalid once type_as_string is called again. */
  716.   error ("cannot convert type `%s'", type_as_string (intype));
  717.   error ("       to type `%s'", type_as_string (reftype));
  718.  
  719.   return error_mark_node;
  720. }
  721.  
  722. /* We are using a reference VAL for its value. Bash that reference all the
  723.    way down to its lowest form. */
  724. tree
  725. convert_from_reference (val)
  726.      tree val;
  727. {
  728.   tree type = TREE_TYPE (val);
  729.  
  730.   if (TREE_CODE (type) == OFFSET_TYPE)
  731.     type = TREE_TYPE (type);
  732.  if (TREE_CODE (type) == REFERENCE_TYPE)
  733.     {
  734.       tree target_type = TREE_TYPE (type);
  735.  
  736.       /* This can happen if we cast to a reference type.  */
  737.       if (TREE_CODE (val) == ADDR_EXPR)
  738.     {
  739.       val = build1 (NOP_EXPR, build_pointer_type (target_type), val);
  740.       val = build_indirect_ref (val, 0);
  741.       return val;
  742.     }
  743.  
  744.       val = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (target_type), val);
  745.  
  746.       TREE_THIS_VOLATILE (val) = TYPE_VOLATILE (target_type);
  747.       TREE_SIDE_EFFECTS (val) = TYPE_VOLATILE (target_type);
  748.       TREE_READONLY (val) = TYPE_READONLY (target_type);
  749.     }
  750.   return val;
  751. }
  752.  
  753. static tree
  754. convert_to_real (type, expr)
  755.      tree type, expr;
  756. {
  757.   register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
  758.  
  759.   if (form == REAL_TYPE)
  760.     return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
  761.           type, expr);
  762.  
  763.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  764.     return build1 (FLOAT_EXPR, type, expr);
  765.  
  766.   my_friendly_assert (form != OFFSET_TYPE, 190);
  767.  
  768.   if (form == POINTER_TYPE)
  769.     error ("pointer value used where a floating point value was expected");
  770.   /* C++: check to see if we can convert this aggregate type
  771.      into the required scalar type.  */
  772.   else if (IS_AGGR_TYPE (TREE_TYPE (expr)))
  773.     {
  774.       tree rval;
  775.       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  776.       if (rval)
  777.     return rval;
  778.       else
  779.     error ("aggregate value used where a floating point value was expected");
  780.     }
  781.  
  782.   {
  783.     register tree tem = make_node (REAL_CST);
  784.     TREE_TYPE (tem) = type;
  785.     TREE_REAL_CST (tem) = REAL_VALUE_ATOF ("0.0");
  786.     return tem;
  787.   }
  788. }
  789.  
  790. /* The result of this is always supposed to be a newly created tree node
  791.    not in use in any existing structure.  */
  792.  
  793. static tree
  794. convert_to_integer (type, expr)
  795.      tree type, expr;
  796. {
  797.   register tree intype = TREE_TYPE (expr);
  798.   register enum tree_code form = TREE_CODE (intype);
  799.   extern tree build_binary_op_nodefault ();
  800.   extern tree build_unary_op ();
  801.  
  802.   if (form == POINTER_TYPE)
  803.     {
  804.       if (integer_zerop (expr))
  805.     expr = integer_zero_node;
  806.       else
  807.     expr = fold (build1 (CONVERT_EXPR,
  808.                  type_for_size (POINTER_SIZE, 0), expr));
  809.       intype = TREE_TYPE (expr);
  810.       form = TREE_CODE (intype);
  811.       if (intype == type)
  812.     return expr;
  813.     }
  814.  
  815.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  816.     {
  817.       register unsigned outprec = TYPE_PRECISION (type);
  818.       register unsigned inprec = TYPE_PRECISION (intype);
  819.       register enum tree_code ex_form = TREE_CODE (expr);
  820.  
  821.       if (flag_int_enum_equivalence == 0
  822.       && TREE_CODE (type) == ENUMERAL_TYPE
  823.       && form == INTEGER_TYPE)
  824.     {
  825.       if (pedantic)
  826.         pedwarn ("anachronistic conversion from integer type to enumeral type `%s'",
  827.              TYPE_NAME_STRING (type));
  828.       if (flag_pedantic_errors)
  829.         return error_mark_node;
  830.     }
  831.  
  832.       /* If we are widening the type, put in an explicit conversion.
  833.      Similarly if we are not changing the width.  However, if this is
  834.      a logical operation that just returns 0 or 1, we can change the
  835.      type of the expression (see below).  */
  836.  
  837.       if (TREE_CODE_CLASS (ex_form) == '<'
  838.       || ex_form == TRUTH_AND_EXPR || ex_form == TRUTH_ANDIF_EXPR
  839.       || ex_form == TRUTH_OR_EXPR || ex_form == TRUTH_ORIF_EXPR
  840.       || ex_form == TRUTH_NOT_EXPR)
  841.     {
  842.       TREE_TYPE (expr) = type;
  843.       return expr;
  844.     }
  845.       else if (outprec >= inprec)
  846.     return build1 (NOP_EXPR, type, expr);
  847.  
  848. /* Here detect when we can distribute the truncation down past some arithmetic.
  849.    For example, if adding two longs and converting to an int,
  850.    we can equally well convert both to ints and then add.
  851.    For the operations handled here, such truncation distribution
  852.    is always safe.
  853.    It is desirable in these cases:
  854.    1) when truncating down to full-word from a larger size
  855.    2) when truncating takes no work.
  856.    3) when at least one operand of the arithmetic has been extended
  857.    (as by C's default conversions).  In this case we need two conversions
  858.    if we do the arithmetic as already requested, so we might as well
  859.    truncate both and then combine.  Perhaps that way we need only one.
  860.  
  861.    Note that in general we cannot do the arithmetic in a type
  862.    shorter than the desired result of conversion, even if the operands
  863.    are both extended from a shorter type, because they might overflow
  864.    if combined in that type.  The exceptions to this--the times when
  865.    two narrow values can be combined in their narrow type even to
  866.    make a wider result--are handled by "shorten" in build_binary_op.  */
  867.  
  868.       switch (ex_form)
  869.     {
  870.     case RSHIFT_EXPR:
  871.       /* We can pass truncation down through right shifting
  872.          when the shift count is a nonpositive constant.  */
  873.       if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
  874.           && tree_int_cst_lt (TREE_OPERAND (expr, 1), integer_one_node))
  875.         goto trunc1;
  876.       break;
  877.  
  878.     case LSHIFT_EXPR:
  879.       /* We can pass truncation down through left shifting
  880.          when the shift count is a nonnegative constant.  */
  881.       if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
  882.           && ! tree_int_cst_lt (TREE_OPERAND (expr, 1), integer_zero_node)
  883.           && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
  884.         {
  885.           /* If shift count is less than the width of the truncated type,
  886.          really shift.  */
  887.           if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
  888.         /* In this case, shifting is like multiplication.  */
  889.         goto trunc1;
  890.           else
  891.         /* If it is >= that width, result is zero.
  892.            Handling this with trunc1 would give the wrong result:
  893.            (int) ((long long) a << 32) is well defined (as 0)
  894.            but (int) a << 32 is undefined and would get a warning.  */
  895.         return convert_to_integer (type, integer_zero_node);
  896.         }
  897.       break;
  898.  
  899.     case MAX_EXPR:
  900.     case MIN_EXPR:
  901.     case MULT_EXPR:
  902.       {
  903.         tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  904.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  905.  
  906.         /* Don't distribute unless the output precision is at least as big
  907.            as the actual inputs.  Otherwise, the comparison of the
  908.            truncated values will be wrong.  */
  909.         if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
  910.         && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
  911.         /* If signedness of arg0 and arg1 don't match,
  912.            we can't necessarily find a type to compare them in.  */
  913.         && (TREE_UNSIGNED (TREE_TYPE (arg0))
  914.             == TREE_UNSIGNED (TREE_TYPE (arg1))))
  915.           goto trunc1;
  916.         break;
  917.       }
  918.  
  919.     case PLUS_EXPR:
  920.     case MINUS_EXPR:
  921.     case BIT_AND_EXPR:
  922.     case BIT_IOR_EXPR:
  923.     case BIT_XOR_EXPR:
  924.     case BIT_ANDTC_EXPR:
  925.     trunc1:
  926.       {
  927.         tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  928.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  929.  
  930.         if (outprec >= BITS_PER_WORD
  931.         || TRULY_NOOP_TRUNCATION (outprec, inprec)
  932.         || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
  933.         || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
  934.           {
  935.         /* Do the arithmetic in type TYPEX,
  936.            then convert result to TYPE.  */
  937.         register tree typex = type;
  938.  
  939.         /* Can't do arithmetic in enumeral types
  940.            so use an integer type that will hold the values.  */
  941.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  942.           typex = type_for_size (TYPE_PRECISION (typex),
  943.                      TREE_UNSIGNED (typex));
  944.  
  945.         /* But now perhaps TYPEX is as wide as INPREC.
  946.            In that case, do nothing special here.
  947.            (Otherwise would recurse infinitely in convert.  */
  948.         if (TYPE_PRECISION (typex) != inprec)
  949.           {
  950.             /* Don't do unsigned arithmetic where signed was wanted,
  951.                or vice versa.
  952.                Exception 1: if we will eventually truncate
  953.                the result, then do the work as unsigned;
  954.                this can prevent unnecessary sign-extension.
  955.                Exception 2: if either of the original operands were
  956.                unsigned then can safely do the work as unsigned.
  957.                And we may need to do it as unsigned
  958.                if we truncate to the original size.  */
  959.             typex = (((outprec <= TYPE_PRECISION (typex)
  960.                    && TREE_UNSIGNED (type))
  961.                   || TREE_UNSIGNED (TREE_TYPE (expr))
  962.                   || TREE_UNSIGNED (TREE_TYPE (arg0))
  963.                   || TREE_UNSIGNED (TREE_TYPE (arg1)))
  964.                  ? unsigned_type (typex) : signed_type (typex));
  965.             return convert (type,
  966.                     build_binary_op_nodefault (ex_form,
  967.                                    convert (typex, arg0),
  968.                                    convert (typex, arg1),
  969.                                    ex_form));
  970.           }
  971.           }
  972.       }
  973.       break;
  974.  
  975.     case NEGATE_EXPR:
  976.     case BIT_NOT_EXPR:
  977.     case ABS_EXPR:
  978.       {
  979.         register tree typex = type;
  980.  
  981.         /* Can't do arithmetic in enumeral types
  982.            so use an integer type that will hold the values.  */
  983.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  984.           typex = type_for_size (TYPE_PRECISION (typex),
  985.                      TREE_UNSIGNED (typex));
  986.  
  987.         /* But now perhaps TYPEX is as wide as INPREC.
  988.            In that case, do nothing special here.
  989.            (Otherwise would recurse infinitely in convert.  */
  990.         if (TYPE_PRECISION (typex) != inprec)
  991.           {
  992.         /* Don't do unsigned arithmetic where signed was wanted,
  993.            or vice versa.  */
  994.         typex = (((outprec <= TYPE_PRECISION (typex)
  995.                && TREE_UNSIGNED (type))
  996.               || TREE_UNSIGNED (TREE_TYPE (expr)))
  997.              ? unsigned_type (typex) : signed_type (typex));
  998.         return convert (type,
  999.                 build_unary_op (ex_form,
  1000.                         convert (typex, TREE_OPERAND (expr, 0)),
  1001.                         1));
  1002.           }
  1003.       }
  1004.  
  1005.     case NOP_EXPR:
  1006.       /* If truncating after truncating, might as well do all at once.
  1007.          If truncating after extending, we may get rid of wasted work.  */
  1008.       return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
  1009.  
  1010.     case COND_EXPR:
  1011.       /* Can treat the two alternative values like the operands
  1012.          of an arithmetic expression.  */
  1013.       {
  1014.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  1015.         tree arg2 = get_unwidened (TREE_OPERAND (expr, 2), type);
  1016.  
  1017.         if (outprec >= BITS_PER_WORD
  1018.         || TRULY_NOOP_TRUNCATION (outprec, inprec)
  1019.         || inprec > TYPE_PRECISION (TREE_TYPE (arg1))
  1020.         || inprec > TYPE_PRECISION (TREE_TYPE (arg2)))
  1021.           {
  1022.         /* Do the arithmetic in type TYPEX,
  1023.            then convert result to TYPE.  */
  1024.         register tree typex = type;
  1025.  
  1026.         /* Can't do arithmetic in enumeral types
  1027.            so use an integer type that will hold the values.  */
  1028.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  1029.           typex = type_for_size (TYPE_PRECISION (typex),
  1030.                      TREE_UNSIGNED (typex));
  1031.  
  1032.         /* But now perhaps TYPEX is as wide as INPREC.
  1033.            In that case, do nothing special here.
  1034.            (Otherwise would recurse infinitely in convert.  */
  1035.         if (TYPE_PRECISION (typex) != inprec)
  1036.           {
  1037.             /* Don't do unsigned arithmetic where signed was wanted,
  1038.                or vice versa.  */
  1039.             typex = (((outprec <= TYPE_PRECISION (typex)
  1040.                    && TREE_UNSIGNED (type))
  1041.                   || TREE_UNSIGNED (TREE_TYPE (expr)))
  1042.                  ? unsigned_type (typex) : signed_type (typex));
  1043.             return convert (type,
  1044.                     fold (build (COND_EXPR, typex,
  1045.                          TREE_OPERAND (expr, 0),
  1046.                          convert (typex, arg1),
  1047.                          convert (typex, arg2))));
  1048.           }
  1049.         else
  1050.           /* It is sometimes worthwhile
  1051.              to push the narrowing down through the conditional.  */
  1052.           return fold (build (COND_EXPR, type,
  1053.                       TREE_OPERAND (expr, 0),
  1054.                       convert (type, TREE_OPERAND (expr, 1)), 
  1055.                       convert (type, TREE_OPERAND (expr, 2))));
  1056.           }
  1057.       }
  1058.  
  1059.     }
  1060.  
  1061.       return build1 (NOP_EXPR, type, expr);
  1062.     }
  1063.  
  1064.   if (form == REAL_TYPE)
  1065.     return build1 (FIX_TRUNC_EXPR, type, expr);
  1066.  
  1067.   if (form == OFFSET_TYPE)
  1068.     error_with_decl (TYPE_NAME (TYPE_OFFSET_BASETYPE (intype)),
  1069.              "pointer-to-member expression object not composed with type `%s' object");
  1070.   else
  1071.     {
  1072.       if (IS_AGGR_TYPE (intype))
  1073.     {
  1074.       tree rval;
  1075.       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  1076.       if (rval) return rval;
  1077.     }
  1078.  
  1079.       error ("aggregate value used where an integer was expected");
  1080.     }
  1081.  
  1082.   {
  1083.     register tree tem = build_int_2 (0, 0);
  1084.     TREE_TYPE (tem) = type;
  1085.     return tem;
  1086.   }
  1087. }
  1088.  
  1089. /* See if there is a constructor of type TYPE which will convert
  1090.    EXPR.  The reference manual seems to suggest (8.5.6) that we need
  1091.    not worry about finding constructors for base classes, then converting
  1092.    to the derived class.
  1093.  
  1094.    MSGP is a pointer to a message that would be an appropriate error
  1095.    string.  If MSGP is NULL, then we are not interested in reporting
  1096.    errors.  */
  1097. tree
  1098. convert_to_aggr (type, expr, msgp, protect)
  1099.      tree type, expr;
  1100.      char **msgp;
  1101.      int protect;
  1102. {
  1103.   tree basetype = type;
  1104.   tree name = TYPE_IDENTIFIER (basetype);
  1105.   tree function, fndecl, fntype, parmtypes, parmlist, result;
  1106.   tree method_name;
  1107.   enum visibility_type visibility;
  1108.   int can_be_private, can_be_protected;
  1109.  
  1110.   if (! TYPE_HAS_CONSTRUCTOR (basetype))
  1111.     {
  1112.       if (msgp)
  1113.     *msgp = "type `%s' does not have a constructor";
  1114.       return error_mark_node;
  1115.     }
  1116.  
  1117.   visibility = visibility_public;
  1118.   can_be_private = 0;
  1119.   can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
  1120.  
  1121.   parmlist = build_tree_list (NULL_TREE, expr);
  1122.   parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
  1123.  
  1124.   if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
  1125.     {
  1126.       parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
  1127.       parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
  1128.     }
  1129.  
  1130.   /* The type of the first argument will be filled in inside the loop.  */
  1131.   parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
  1132.   parmtypes = tree_cons (NULL_TREE, TYPE_POINTER_TO (basetype), parmtypes);
  1133.  
  1134.   method_name = build_decl_overload (name, parmtypes, 1);
  1135.  
  1136.   /* constructors are up front.  */
  1137.   fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
  1138.   if (TYPE_HAS_DESTRUCTOR (basetype))
  1139.     fndecl = DECL_CHAIN (fndecl);
  1140.  
  1141.   while (fndecl)
  1142.     {
  1143.       if (DECL_ASSEMBLER_NAME (fndecl) == method_name)
  1144.     {
  1145.       function = fndecl;
  1146.       if (protect)
  1147.         {
  1148.           if (TREE_PRIVATE (fndecl))
  1149.         {
  1150.           can_be_private =
  1151.             (basetype == current_class_type
  1152.              || is_friend (basetype, current_function_decl)
  1153.              || purpose_member (basetype, DECL_VISIBILITY (fndecl)));
  1154.           if (! can_be_private)
  1155.             goto found;
  1156.         }
  1157.           else if (TREE_PROTECTED (fndecl))
  1158.         {
  1159.           if (! can_be_protected)
  1160.             goto found;
  1161.         }
  1162.         }
  1163.       goto found_and_ok;
  1164.     }
  1165.       fndecl = DECL_CHAIN (fndecl);
  1166.     }
  1167.  
  1168.   /* No exact conversion was found.  See if an approximate
  1169.      one will do.  */
  1170.   fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
  1171.   if (TYPE_HAS_DESTRUCTOR (basetype))
  1172.     fndecl = DECL_CHAIN (fndecl);
  1173.  
  1174.   {
  1175.     int saw_private = 0;
  1176.     int saw_protected = 0;
  1177.     struct candidate *candidates =
  1178.       (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
  1179.     struct candidate *cp = candidates;
  1180.  
  1181.     while (fndecl)
  1182.       {
  1183.     function = fndecl;
  1184.     cp->harshness = (unsigned short *)alloca (3 * sizeof (short));
  1185.     compute_conversion_costs (fndecl, parmlist, cp, 2);
  1186.     if (cp->evil == 0)
  1187.       {
  1188.         cp->u.field = fndecl;
  1189.         if (protect)
  1190.           {
  1191.         if (TREE_PRIVATE (fndecl))
  1192.           visibility = visibility_private;
  1193.         else if (TREE_PROTECTED (fndecl))
  1194.           visibility = visibility_protected;
  1195.         else
  1196.           visibility = visibility_public;
  1197.           }
  1198.         else
  1199.           visibility = visibility_public;
  1200.  
  1201.         if (visibility == visibility_private
  1202.         ? (basetype == current_class_type
  1203.            || is_friend (basetype, cp->function)
  1204.            || purpose_member (basetype, DECL_VISIBILITY (fndecl)))
  1205.         : visibility == visibility_protected
  1206.         ? (can_be_protected
  1207.            || purpose_member (basetype, DECL_VISIBILITY (fndecl)))
  1208.         : 1)
  1209.           {
  1210.         if (cp->user == 0 && cp->b_or_d == 0
  1211.             && cp->easy <= 1)
  1212.           {
  1213.             goto found_and_ok;
  1214.           }
  1215.         cp++;
  1216.           }
  1217.         else
  1218.           {
  1219.         if (visibility == visibility_private)
  1220.           saw_private = 1;
  1221.         else
  1222.           saw_protected = 1;
  1223.           }
  1224.       }
  1225.     fndecl = DECL_CHAIN (fndecl);
  1226.       }
  1227.     if (cp - candidates)
  1228.       {
  1229.     /* Rank from worst to best.  Then cp will point to best one.
  1230.        Private fields have their bits flipped.  For unsigned
  1231.        numbers, this should make them look very large.
  1232.        If the best alternate has a (signed) negative value,
  1233.        then all we ever saw were private members.  */
  1234.     if (cp - candidates > 1)
  1235.       qsort (candidates,    /* char *base */
  1236.          cp - candidates, /* int nel */
  1237.          sizeof (struct candidate), /* int width */
  1238.          rank_for_overload); /* int (*compar)() */
  1239.  
  1240.     --cp;
  1241.     if (cp->evil > 1)
  1242.       {
  1243.         if (msgp)
  1244.           *msgp = "ambiguous type conversion possible for `%s'";
  1245.         return error_mark_node;
  1246.       }
  1247.  
  1248.     function = cp->function;
  1249.     fndecl = cp->u.field;
  1250.     goto found_and_ok;
  1251.       }
  1252.     else if (msgp)
  1253.       {
  1254.     if (saw_private)
  1255.       if (saw_protected)
  1256.         *msgp = "only private and protected conversions apply";
  1257.       else
  1258.         *msgp = "only private conversions apply";
  1259.     else if (saw_protected)
  1260.       *msgp = "only protected conversions apply";
  1261.       }
  1262.     return error_mark_node;
  1263.   }
  1264.   /* NOTREACHED */
  1265.  
  1266.  not_found:
  1267.   if (msgp) *msgp = "no appropriate conversion to type `%s'";
  1268.   return error_mark_node;
  1269.  found:
  1270.   if (visibility == visibility_private)
  1271.     if (! can_be_private)
  1272.       {
  1273.     if (msgp)
  1274.       *msgp = TREE_PRIVATE (fndecl)
  1275.         ? "conversion to type `%s' is private"
  1276.         : "conversion to type `%s' is from private base class";
  1277.     return error_mark_node;
  1278.       }
  1279.   if (visibility == visibility_protected)
  1280.     if (! can_be_protected)
  1281.       {
  1282.     if (msgp)
  1283.       *msgp = TREE_PRIVATE (fndecl)
  1284.         ? "conversion to type `%s' is protected"
  1285.         : "conversion to type `%s' is from protected base class";
  1286.     return error_mark_node;
  1287.       }
  1288.   function = fndecl;
  1289.  found_and_ok:
  1290.  
  1291.   /* It will convert, but we don't do anything about it yet.  */
  1292.   if (msgp == 0)
  1293.     return NULL_TREE;
  1294.  
  1295.   fntype = TREE_TYPE (function);
  1296.   if (DECL_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL)
  1297.     function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
  1298.   else
  1299.     function = default_conversion (function);
  1300.  
  1301.   result = build_nt (CALL_EXPR, function,
  1302.              convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
  1303.                     parmlist, NULL_TREE, LOOKUP_NORMAL),
  1304.              NULL_TREE);
  1305.   TREE_TYPE (result) = TREE_TYPE (fntype);
  1306.   TREE_SIDE_EFFECTS (result) = 1;
  1307.   TREE_RAISES (result) = !! TYPE_RAISES_EXCEPTIONS (fntype);
  1308.   return result;
  1309. }
  1310.  
  1311. /* Call this when we know (for any reason) that expr is
  1312.    not, in fact, zero.  */
  1313. tree
  1314. convert_pointer_to (binfo, expr)
  1315.      tree binfo, expr;
  1316. {
  1317.   register tree intype = TREE_TYPE (expr);
  1318.   tree ptr_type;
  1319.   tree type, rval;
  1320.  
  1321.   if (TREE_CODE (binfo) == TREE_VEC)
  1322.     type = BINFO_TYPE (binfo);
  1323.   else if (IS_AGGR_TYPE (binfo))
  1324.     {
  1325.       type = binfo;
  1326.       binfo = TYPE_BINFO (binfo);
  1327.     }
  1328.   else
  1329.     {
  1330.       type = binfo;
  1331.       binfo = NULL_TREE;
  1332.     }
  1333.  
  1334.   ptr_type = build_pointer_type (type);
  1335.   if (ptr_type == TYPE_MAIN_VARIANT (intype))
  1336.     return expr;
  1337.  
  1338.   if (intype == error_mark_node)
  1339.     return error_mark_node;
  1340.  
  1341.   my_friendly_assert (!integer_zerop (expr), 191);
  1342.  
  1343.   if (IS_AGGR_TYPE (type)
  1344.       && IS_AGGR_TYPE (TREE_TYPE (intype))
  1345.       && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
  1346.     {
  1347.       tree path;
  1348.       int distance = get_base_distance (type, TYPE_MAIN_VARIANT (TREE_TYPE (intype)), 0, &path);
  1349.  
  1350.       /* This function shouldn't be called with unqualified arguments
  1351.      but if it is, give them an error message that they can read.
  1352.      */
  1353.       if (distance < 0)
  1354.     {
  1355.       error ("cannot convert a pointer of type `%s'",
  1356.          TYPE_NAME_STRING (TREE_TYPE (intype)));
  1357.       error_with_aggr_type (type, "to a pointer of type `%s'");
  1358.  
  1359.       return error_mark_node;
  1360.     }
  1361.  
  1362.       return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
  1363.     }
  1364.   rval = build1 (NOP_EXPR, ptr_type,
  1365.          TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
  1366.   TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
  1367.   return rval;
  1368. }
  1369.  
  1370. #ifdef MPW
  1371. #pragma segment CPCVT02
  1372. #endif
  1373.  
  1374. /* Same as above, but don't abort if we get an "ambiguous" baseclass.
  1375.    There's only one virtual baseclass we are looking for, and once
  1376.    we find one such virtual baseclass, we have found them all.  */
  1377.  
  1378. tree
  1379. convert_pointer_to_vbase (binfo, expr)
  1380.      tree binfo;
  1381.      tree expr;
  1382. {
  1383.   tree intype = TREE_TYPE (TREE_TYPE (expr));
  1384.   tree binfos = TYPE_BINFO_BASETYPES (intype);
  1385.   int i;
  1386.  
  1387.   for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
  1388.     {
  1389.       tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
  1390.       if (BINFO_TYPE (binfo) == basetype)
  1391.     return convert_pointer_to (binfo, expr);
  1392.       if (binfo_member (BINFO_TYPE (binfo), CLASSTYPE_VBASECLASSES (basetype)))
  1393.     return convert_pointer_to_vbase (binfo, convert_pointer_to (basetype, expr));
  1394.     }
  1395.   my_friendly_abort (6);
  1396.   /* NOTREACHED */
  1397.   return NULL_TREE;
  1398. }
  1399.  
  1400. /* Create an expression whose value is that of EXPR,
  1401.    converted to type TYPE.  The TREE_TYPE of the value
  1402.    is always TYPE.  This function implements all reasonable
  1403.    conversions; callers should filter out those that are
  1404.    not permitted by the language being compiled.  */
  1405.  
  1406. tree
  1407. convert (type, expr)
  1408.      tree type, expr;
  1409. {
  1410.   register tree e = expr;
  1411.   register enum tree_code code = TREE_CODE (type);
  1412.  
  1413.   if (type == TREE_TYPE (expr)
  1414.       || TREE_CODE (expr) == ERROR_MARK)
  1415.     return expr;
  1416.   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
  1417.     return fold (build1 (NOP_EXPR, type, expr));
  1418.   if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
  1419.     return error_mark_node;
  1420.   if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
  1421.     {
  1422.       error ("void value not ignored as it ought to be");
  1423.       return error_mark_node;
  1424.     }
  1425.   if (code == VOID_TYPE)
  1426.     {
  1427.       tree rval = build_type_conversion (NOP_EXPR, type, e, 0);
  1428.       /* If we can convert to void type via a type conversion, do so.  */
  1429.       if (rval)
  1430.     return rval;
  1431.       return build1 (CONVERT_EXPR, type, e);
  1432.     }
  1433. #if 0
  1434.   /* This is incorrect.  A truncation can't be stripped this way.
  1435.      Extensions will be stripped by the use of get_unwidened.  */
  1436.   if (TREE_CODE (expr) == NOP_EXPR)
  1437.     return convert (type, TREE_OPERAND (expr, 0));
  1438. #endif
  1439.  
  1440.   /* Just convert to the type of the member.  */
  1441.   if (code == OFFSET_TYPE)
  1442.     {
  1443.       type = TREE_TYPE (type);
  1444.       code = TREE_CODE (type);
  1445.     }
  1446.  
  1447.   /* C++ */
  1448.   if (code == REFERENCE_TYPE)
  1449.     return fold (convert_to_reference (error_mark_node,
  1450.                        type, e,
  1451.                        NULL_TREE, -1, (char *)NULL,
  1452.                        -1, LOOKUP_NORMAL));
  1453.   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
  1454.     e = convert_from_reference (e);
  1455.  
  1456.   if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
  1457.     return fold (convert_to_integer (type, e));
  1458.   if (code == POINTER_TYPE)
  1459.     return fold (convert_to_pointer (type, e));
  1460.   if (code == REAL_TYPE)
  1461.     return fold (convert_to_real (type, e));
  1462.  
  1463.   /* New C++ semantics:  since assignment is now based on
  1464.      memberwise copying,  if the rhs type is derived from the
  1465.      lhs type, then we may still do a conversion.  */
  1466.   if (IS_AGGR_TYPE_CODE (code))
  1467.     {
  1468.       tree dtype = TREE_TYPE (e);
  1469.  
  1470.       if (TREE_CODE (dtype) == REFERENCE_TYPE)
  1471.     {
  1472.       e = convert_from_reference (e);
  1473.       dtype = TREE_TYPE (e);
  1474.     }
  1475.       dtype = TYPE_MAIN_VARIANT (dtype);
  1476.  
  1477.       /* Conversion between aggregate types.  New C++ semantics allow
  1478.      objects of derived type to be cast to objects of base type.
  1479.      Old semantics only allowed this between pointers.
  1480.  
  1481.      There may be some ambiguity between using a constructor
  1482.      vs. using a type conversion operator when both apply.  */
  1483.  
  1484.       if (IS_AGGR_TYPE (dtype))
  1485.     {
  1486.       tree binfo;
  1487.  
  1488.       tree conversion = TYPE_HAS_CONVERSION (dtype)
  1489.         ? build_type_conversion (CONVERT_EXPR, type, e, 1) : NULL_TREE;
  1490.  
  1491.       if (TYPE_HAS_CONSTRUCTOR (type))
  1492.         {
  1493.           tree rval = build_method_call (NULL_TREE, constructor_name (type),
  1494.                          build_tree_list (NULL_TREE, e),
  1495.                          TYPE_BINFO (type),
  1496.                          conversion ? LOOKUP_NO_CONVERSION : 0);
  1497.  
  1498.           if (rval != error_mark_node)
  1499.         {
  1500.           if (conversion)
  1501.             {
  1502.               error ("both constructor and type conversion operator apply");
  1503.               return error_mark_node;
  1504.             }
  1505.           /* call to constructor successful.  */
  1506.           rval = build_cplus_new (type, rval, 0);
  1507.           return rval;
  1508.         }
  1509.         }
  1510.       /* Type conversion successful/applies.  */
  1511.       if (conversion)
  1512.         {
  1513.           if (conversion == error_mark_node)
  1514.         error ("ambiguous pointer conversion");
  1515.           return conversion;
  1516.         }
  1517.  
  1518.       /* now try normal C++ assignment semantics.  */
  1519.       binfo = TYPE_BINFO (dtype);
  1520.       if (BINFO_TYPE (binfo) == type
  1521.           || (binfo = get_binfo (type, dtype, 1)))
  1522.         {
  1523.           if (binfo == error_mark_node)
  1524.         return error_mark_node;
  1525.         }
  1526.       if (binfo != NULL_TREE)
  1527.         {
  1528.           if (lvalue_p (e))
  1529.         {
  1530.           e = build_unary_op (ADDR_EXPR, e, 0);
  1531.  
  1532.           if (! BINFO_OFFSET_ZEROP (binfo))
  1533.             e = build (PLUS_EXPR, TYPE_POINTER_TO (type),
  1534.                    e, BINFO_OFFSET (binfo));
  1535.           return build1 (INDIRECT_REF, type, e);
  1536.         }
  1537.  
  1538.           sorry ("addressable aggregates");
  1539.           return error_mark_node;
  1540.         }
  1541.       error ("conversion between incompatible aggregate types requested");
  1542.       return error_mark_node;
  1543.     }
  1544.       /* conversion from non-aggregate to aggregate type requires constructor.  */
  1545.       else if (TYPE_HAS_CONSTRUCTOR (type))
  1546.     {
  1547.       tree rval;
  1548.       tree init = build_method_call (NULL_TREE, constructor_name (type),
  1549.                      build_tree_list (NULL_TREE, e),
  1550.                      TYPE_BINFO (type), LOOKUP_NORMAL);
  1551.       if (init == error_mark_node)
  1552.         {
  1553.           error_with_aggr_type (type, "in conversion to type `%s'");
  1554.           return error_mark_node;
  1555.         }
  1556.       rval = build_cplus_new (type, init, 0);
  1557.       return rval;
  1558.     }
  1559.     }
  1560.  
  1561.   /* If TYPE or TREE_TYPE (EXPR) is not on the permanent_obstack,
  1562.      then the it won't be hashed and hence compare as not equal,
  1563.      even when it is.  */
  1564.   if (code == ARRAY_TYPE
  1565.       && TREE_TYPE (TREE_TYPE (expr)) == TREE_TYPE (type)
  1566.       && index_type_equal (TYPE_DOMAIN (TREE_TYPE (expr)), TYPE_DOMAIN (type)))
  1567.     return expr;
  1568.  
  1569.   error ("conversion to non-scalar type requested");
  1570.   return error_mark_node;
  1571. }
  1572.  
  1573. /* Like convert, except permit conversions to take place which
  1574.    are not normally allowed due to visibility restrictions
  1575.    (such as conversion from sub-type to private super-type).  */
  1576. tree
  1577. convert_force (type, expr)
  1578.      tree type;
  1579.      tree expr;
  1580. {
  1581.   register tree e = expr;
  1582.   register enum tree_code code = TREE_CODE (type);
  1583.  
  1584.   if (code == REFERENCE_TYPE)
  1585.     return fold (convert_to_reference (0, type, e,
  1586.                        NULL_TREE, -1, (char *)NULL,
  1587.                        -1, 0));
  1588.   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
  1589.     e = convert_from_reference (e);
  1590.  
  1591.   if (code == POINTER_TYPE)
  1592.     return fold (convert_to_pointer_force (type, e));
  1593.  
  1594.   {
  1595.     int old_equiv = flag_int_enum_equivalence;
  1596.     flag_int_enum_equivalence = 1;
  1597.     e = convert (type, e);
  1598.     flag_int_enum_equivalence = old_equiv;
  1599.   }
  1600.   return e;
  1601. }
  1602.  
  1603. /* Subroutine of build_type_conversion.  */
  1604. static tree
  1605. build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
  1606.      tree xtype, basetype;
  1607.      tree expr;
  1608.      tree typename;
  1609.      int for_sure;
  1610. {
  1611.   tree first_arg = expr;
  1612.   tree rval;
  1613.   int flags;
  1614.  
  1615.   if (for_sure == 0)
  1616.     {
  1617.       if (! lvalue_p (expr))
  1618.     first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node);
  1619.       flags = LOOKUP_PROTECT;
  1620.     }
  1621.   else
  1622.     flags = LOOKUP_NORMAL;
  1623.  
  1624.   rval = build_method_call (first_arg, constructor_name (typename),
  1625.                 NULL_TREE, NULL_TREE, flags);
  1626.   if (rval == error_mark_node)
  1627.     {
  1628.       if (for_sure == 0)
  1629.     return NULL_TREE;
  1630.       return error_mark_node;
  1631.     }
  1632.   if (first_arg != expr)
  1633.     {
  1634.       expr = build_up_reference (build_reference_type (TREE_TYPE (expr)), expr,
  1635.                  LOOKUP_COMPLAIN, 1);
  1636.       TREE_VALUE (TREE_OPERAND (rval, 1)) = build_unary_op (ADDR_EXPR, expr, 0);
  1637.     }
  1638.   if (TREE_CODE (TREE_TYPE (rval)) == REFERENCE_TYPE
  1639.       && TREE_CODE (xtype) != REFERENCE_TYPE)
  1640.     rval = default_conversion (rval);
  1641.  
  1642.   if (pedantic
  1643.       && TREE_TYPE (xtype)
  1644.       && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
  1645.       > TREE_READONLY (TREE_TYPE (xtype))))
  1646.     pedwarn ("user-defined conversion casting away `const'");
  1647.   return convert (xtype, rval);
  1648. }
  1649.  
  1650. #ifdef MPW
  1651. #pragma segment CPCVT03
  1652. #endif
  1653.  
  1654. /* Convert an aggregate EXPR to type XTYPE.  If a conversion
  1655.    exists, return the attempted conversion.  This may
  1656.    return ERROR_MARK_NODE if the conversion is not
  1657.    allowed (references private members, etc).
  1658.    If no conversion exists, NULL_TREE is returned.
  1659.  
  1660.    If (FOR_SURE & 1) is non-zero, then we allow this type conversion
  1661.    to take place immediately.  Otherwise, we build a SAVE_EXPR
  1662.    which can be evaluated if the results are ever needed.
  1663.  
  1664.    If FOR_SURE >= 2, then we only look for exact conversions.
  1665.  
  1666.    TYPE may be a reference type, in which case we first look
  1667.    for something that will convert to a reference type.  If
  1668.    that fails, we will try to look for something of the
  1669.    reference's target type, and then return a reference to that.  */
  1670. tree
  1671. build_type_conversion (code, xtype, expr, for_sure)
  1672.      enum tree_code code;
  1673.      tree xtype, expr;
  1674.      int for_sure;
  1675. {
  1676.   /* C++: check to see if we can convert this aggregate type
  1677.      into the required scalar type.  */
  1678.   tree type, type_default;
  1679.   tree typename = build_typename_overload (xtype), *typenames;
  1680.   int n_variants = 0;
  1681.   tree basetype, save_basetype;
  1682.   tree rval;
  1683.   int exact_conversion = for_sure >= 2;
  1684.   for_sure &= 1;
  1685.  
  1686.   if (expr == error_mark_node)
  1687.     return error_mark_node;
  1688.  
  1689.   basetype = TREE_TYPE (expr);
  1690.   if (TREE_CODE (basetype) == REFERENCE_TYPE)
  1691.     basetype = TREE_TYPE (basetype);
  1692.  
  1693.   basetype = TYPE_MAIN_VARIANT (basetype);
  1694.   if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
  1695.     return 0;
  1696.  
  1697.   if (TREE_CODE (xtype) == POINTER_TYPE
  1698.       || TREE_CODE (xtype) == REFERENCE_TYPE)
  1699.     {
  1700.       /* Prepare to match a variant of this type.  */
  1701.       type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
  1702.       for (n_variants = 0; type; type = TYPE_NEXT_VARIANT (type))
  1703.     n_variants++;
  1704.       typenames = (tree *)alloca (n_variants * sizeof (tree));
  1705.       for (n_variants = 0, type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
  1706.        type; n_variants++, type = TYPE_NEXT_VARIANT (type))
  1707.     {
  1708.       if (type == TREE_TYPE (xtype))
  1709.         typenames[n_variants] = typename;
  1710.       else if (TREE_CODE (xtype) == POINTER_TYPE)
  1711.         typenames[n_variants] = build_typename_overload (build_pointer_type (type));
  1712.       else
  1713.         typenames[n_variants] = build_typename_overload (build_reference_type (type));
  1714.     }
  1715.     }
  1716.  
  1717.   save_basetype = basetype;
  1718.   type = xtype;
  1719.  
  1720.   while (TYPE_HAS_CONVERSION (basetype))
  1721.     {
  1722.       int i;
  1723.       if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
  1724.     return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1725.       for (i = 0; i < n_variants; i++)
  1726.     if (typenames[i] != typename
  1727.         && lookup_fnfields (TYPE_BINFO (basetype), typenames[i], 0))
  1728.       return build_type_conversion_1 (xtype, basetype, expr, typenames[i], for_sure);
  1729.  
  1730.       if (TYPE_BINFO_BASETYPES (basetype))
  1731.     basetype = TYPE_BINFO_BASETYPE (basetype, 0);
  1732.       else break;
  1733.     }
  1734.  
  1735.   if (TREE_CODE (type) == REFERENCE_TYPE)
  1736.     {
  1737.       tree first_arg = expr;
  1738.       type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  1739.       basetype = save_basetype;
  1740.  
  1741.       /* May need to build a temporary for this.  */
  1742.       while (TYPE_HAS_CONVERSION (basetype))
  1743.     {
  1744.       if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
  1745.         {
  1746.           int flags;
  1747.  
  1748.           if (for_sure == 0)
  1749.         {
  1750.           if (! lvalue_p (expr))
  1751.             first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node);
  1752.           flags = LOOKUP_PROTECT;
  1753.         }
  1754.           else
  1755.         flags = LOOKUP_NORMAL;
  1756.           rval = build_method_call (first_arg, constructor_name (typename),
  1757.                     NULL_TREE, NULL_TREE, flags);
  1758.           if (rval == error_mark_node)
  1759.         {
  1760.           if (for_sure == 0)
  1761.             return NULL_TREE;
  1762.           return error_mark_node;
  1763.         }
  1764.           TREE_VALUE (TREE_OPERAND (rval, 1)) = expr;
  1765.  
  1766.           if (IS_AGGR_TYPE (type))
  1767.         {
  1768.           tree init = build_method_call (NULL_TREE,
  1769.                          constructor_name (type),
  1770.                          build_tree_list (NULL_TREE, rval), NULL_TREE, LOOKUP_NORMAL);
  1771.           tree temp = build_cplus_new (type, init, 1);
  1772.           return build_up_reference (TYPE_REFERENCE_TO (type), temp,
  1773.                          LOOKUP_COMPLAIN, 1);
  1774.         }
  1775.           return convert (xtype, rval);
  1776.         }
  1777.       if (TYPE_BINFO_BASETYPES (basetype))
  1778.         basetype = TYPE_BINFO_BASETYPE (basetype, 0);
  1779.       else break;
  1780.     }
  1781.       /* No free conversions for reference types, right?.  */
  1782.       return NULL_TREE;
  1783.     }
  1784.  
  1785.   if (exact_conversion)
  1786.     return NULL_TREE;
  1787.  
  1788.   /* No perfect match found, try default.  */
  1789.   if (code == CONVERT_EXPR && TREE_CODE (type) == POINTER_TYPE)
  1790.     type_default = ptr_type_node;
  1791.   else if (type == void_type_node)
  1792.     return NULL_TREE;
  1793.   else
  1794.     {
  1795.       extern tree default_conversion ();
  1796.       tree tmp = default_conversion (build1 (NOP_EXPR, type, integer_zero_node));
  1797.       if (tmp == error_mark_node)
  1798.     return NULL_TREE;
  1799.       type_default = TREE_TYPE (tmp);
  1800.     }
  1801.  
  1802.   basetype = save_basetype;
  1803.  
  1804.   if (type_default != type)
  1805.     {
  1806.       type = type_default;
  1807.       typename = build_typename_overload (type);
  1808.  
  1809.       while (TYPE_HAS_CONVERSION (basetype))
  1810.     {
  1811.       if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
  1812.         return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1813.       if (TYPE_BINFO_BASETYPES (basetype))
  1814.         basetype = TYPE_BINFO_BASETYPE (basetype, 0);
  1815.       else break;
  1816.     }
  1817.     }
  1818.  
  1819.  try_pointer:
  1820.  
  1821.   if (type == ptr_type_node)
  1822.     {
  1823.       /* Try converting to some other pointer type
  1824.      with which void* is compatible, or in situations
  1825.      in which void* is appropriate (such as &&,||, and !).  */
  1826.  
  1827.       while (TYPE_HAS_CONVERSION (basetype))
  1828.     {
  1829.       if (CLASSTYPE_CONVERSION (basetype, ptr_conv) != 0)
  1830.         {
  1831.           if (CLASSTYPE_CONVERSION (basetype, ptr_conv) == error_mark_node)
  1832.         return error_mark_node;
  1833.           typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, ptr_conv));
  1834.           return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1835.         }
  1836.       if (TYPE_BINFO_BASETYPES (basetype))
  1837.         basetype = TYPE_BINFO_BASETYPE (basetype, 0);
  1838.       else break;
  1839.     }
  1840.     }
  1841.   if (TREE_CODE (type) == POINTER_TYPE
  1842.       && TYPE_READONLY (TREE_TYPE (type))
  1843.       && TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)
  1844.     {
  1845.       /* Try converting to some other pointer type
  1846.      with which const void* is compatible.  */
  1847.  
  1848.       while (TYPE_HAS_CONVERSION (basetype))
  1849.     {
  1850.       if (CLASSTYPE_CONVERSION (basetype, constptr_conv) != 0)
  1851.         {
  1852.           if (CLASSTYPE_CONVERSION (basetype, constptr_conv) == error_mark_node)
  1853.         return error_mark_node;
  1854.           typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, constptr_conv));
  1855.           return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1856.         }
  1857.       if (TYPE_BINFO_BASETYPES (basetype))
  1858.         basetype = TYPE_BINFO_BASETYPE (basetype, 0);
  1859.       else break;
  1860.     }
  1861.     }
  1862.   /* Use the longer or shorter conversion that is appropriate.  Have
  1863.      to check against 0 because the conversion may come from a baseclass.  */
  1864.   if (TREE_CODE (type) == INTEGER_TYPE
  1865.       && TYPE_HAS_INT_CONVERSION (basetype)
  1866.       && CLASSTYPE_CONVERSION (basetype, int_conv) != 0
  1867.       && CLASSTYPE_CONVERSION (basetype, int_conv) != error_mark_node)
  1868.     {
  1869.       typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, int_conv));
  1870.       return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1871.     }
  1872.   if (TREE_CODE (type) == REAL_TYPE
  1873.       && TYPE_HAS_REAL_CONVERSION (basetype)
  1874.       && CLASSTYPE_CONVERSION (basetype, real_conv) != 0
  1875.       && CLASSTYPE_CONVERSION (basetype, real_conv) != error_mark_node)
  1876.     {
  1877.       typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, real_conv));
  1878.       return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1879.     }
  1880.  
  1881.   /* THIS IS A KLUDGE.  */
  1882.   if (TREE_CODE (type) != POINTER_TYPE
  1883.       && (code == TRUTH_ANDIF_EXPR
  1884.       || code == TRUTH_ORIF_EXPR
  1885.       || code == TRUTH_NOT_EXPR))
  1886.     {
  1887.       /* Here's when we can convert to a pointer.  */
  1888.       type = ptr_type_node;
  1889.       goto try_pointer;
  1890.     }
  1891.  
  1892.   /* THESE ARE TOTAL KLUDGES.  */
  1893.   /* Default promotion yields no new alternatives, try
  1894.      conversions which are anti-default, such as
  1895.  
  1896.      double -> float or int -> unsigned or unsigned -> long
  1897.  
  1898.      */
  1899.   if (type_default == type
  1900.       && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == REAL_TYPE))
  1901.     {
  1902.       int not_again = 0;
  1903.  
  1904.       if (type == double_type_node)
  1905.     typename = build_typename_overload (float_type_node);
  1906.       else if (type == integer_type_node)
  1907.     typename = build_typename_overload (unsigned_type_node);
  1908.       else if (type == unsigned_type_node)
  1909.     typename = build_typename_overload (long_integer_type_node);
  1910.  
  1911.     again:
  1912.       basetype = save_basetype;
  1913.       while (TYPE_HAS_CONVERSION (basetype))
  1914.     {
  1915.       if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
  1916.         return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1917.       if (TYPE_BINFO_BASETYPES (basetype))
  1918.         basetype = TYPE_BINFO_BASETYPE (basetype, 0);
  1919.       else break;
  1920.     }
  1921.       if (! not_again)
  1922.     {
  1923.       if (type == integer_type_node)
  1924.         {
  1925.           typename = build_typename_overload (long_integer_type_node);
  1926.           not_again = 1;
  1927.           goto again;
  1928.         }
  1929.       else
  1930.         {
  1931.           typename = build_typename_overload (integer_type_node);
  1932.           not_again = 1;
  1933.           goto again;
  1934.         }
  1935.     }
  1936.     }
  1937.  
  1938.   /* Now, try C promotions...
  1939.  
  1940.      float -> int
  1941.      int -> float, void *
  1942.      void * -> int
  1943.  
  1944.      Truthvalue conversions let us try to convert
  1945.      to pointer if we were going for int, and to int
  1946.      if we were looking for pointer.  */
  1947.  
  1948.     basetype = save_basetype;
  1949.     if (TREE_CODE (type) == REAL_TYPE
  1950.     || (TREE_CODE (type) == POINTER_TYPE
  1951.         && (code == TRUTH_ANDIF_EXPR
  1952.         || code == TRUTH_ORIF_EXPR
  1953.         || code == TRUTH_NOT_EXPR)))
  1954.       type = integer_type_node;
  1955.     else if (TREE_CODE (type) == INTEGER_TYPE)
  1956.       if (TYPE_HAS_REAL_CONVERSION (basetype))
  1957.     type = double_type_node;
  1958.       else
  1959.     return NULL_TREE;
  1960.     else
  1961.       return NULL_TREE;
  1962.  
  1963.     typename = build_typename_overload (type);
  1964.     while (TYPE_HAS_CONVERSION (basetype))
  1965.       {
  1966.     if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
  1967.       {
  1968.         rval = build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1969.         return rval;
  1970.       }
  1971.     if (TYPE_BINFO_BASETYPES (basetype))
  1972.       basetype = TYPE_BINFO_BASETYPE (basetype, 0);
  1973.     else
  1974.       break;
  1975.       }
  1976.  
  1977.   return NULL_TREE;
  1978. }
  1979.  
  1980. /* Must convert two aggregate types to non-aggregate type.
  1981.    Attempts to find a non-ambiguous, "best" type conversion.
  1982.  
  1983.    Return 1 on success, 0 on failure.
  1984.  
  1985.    @@ What are the real semantics of this supposed to be??? */
  1986. int
  1987. build_default_binary_type_conversion (code, arg1, arg2)
  1988.      enum tree_code code;
  1989.      tree *arg1, *arg2;
  1990. {
  1991.   tree type1 = TREE_TYPE (*arg1);
  1992.   tree type2 = TREE_TYPE (*arg2);
  1993.   char *name1, *name2;
  1994.  
  1995.   if (TREE_CODE (type1) == REFERENCE_TYPE)
  1996.     type1 = TREE_TYPE (type1);
  1997.   if (TREE_CODE (type2) == REFERENCE_TYPE)
  1998.     type2 = TREE_TYPE (type2);
  1999.  
  2000.   if (TREE_CODE (TYPE_NAME (type1)) != TYPE_DECL)
  2001.     {
  2002.       tree decl = typedecl_for_tag (type1);
  2003.       if (decl)
  2004.     error ("type conversion nonexistent for type `%s'",
  2005.            IDENTIFIER_POINTER (DECL_NAME (decl)));
  2006.       else
  2007.     error ("type conversion nonexistent for non-C++ type");
  2008.       return 0;
  2009.     }
  2010.   if (TREE_CODE (TYPE_NAME (type2)) != TYPE_DECL)
  2011.     {
  2012.       tree decl = typedecl_for_tag (type2);
  2013.       if (decl)
  2014.     error ("type conversion nonexistent for type `%s'",
  2015.            IDENTIFIER_POINTER (decl));
  2016.       else
  2017.     error ("type conversion nonexistent for non-C++ type");
  2018.       return 0;
  2019.     }
  2020.  
  2021.   name1 = TYPE_NAME_STRING (type1);
  2022.   name2 = TYPE_NAME_STRING (type2);
  2023.  
  2024.   if (!IS_AGGR_TYPE (type1) || !TYPE_HAS_CONVERSION (type1))
  2025.     {
  2026.       if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2))
  2027.     error ("type conversion required for binary operation on types `%s' and `%s'",
  2028.            name1, name2);
  2029.       else
  2030.     error ("type conversion required for type `%s'", name1);
  2031.       return 0;
  2032.     }
  2033.   else if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2))
  2034.     {
  2035.       error ("type conversion required for type `%s'", name2);
  2036.       return 0;
  2037.     }
  2038.  
  2039.   if (TYPE_HAS_INT_CONVERSION (type1) && TYPE_HAS_REAL_CONVERSION (type1))
  2040.     warning ("ambiguous type conversion for type `%s', defaulting to int", name1);
  2041.   if (TYPE_HAS_INT_CONVERSION (type1))
  2042.     {
  2043.       *arg1 = build_type_conversion (code, integer_type_node, *arg1, 1);
  2044.       *arg2 = build_type_conversion (code, integer_type_node, *arg2, 1);
  2045.     }
  2046.   else if (TYPE_HAS_REAL_CONVERSION (type1))
  2047.     {
  2048.       *arg1 = build_type_conversion (code, double_type_node, *arg1, 1);
  2049.       *arg2 = build_type_conversion (code, double_type_node, *arg2, 1);
  2050.     }
  2051.   else
  2052.     {
  2053.       *arg1 = build_type_conversion (code, ptr_type_node, *arg1, 1);
  2054.       if (*arg1 == error_mark_node)
  2055.     error ("ambiguous pointer conversion");
  2056.       *arg2 = build_type_conversion (code, ptr_type_node, *arg2, 1);
  2057.       if (*arg1 != error_mark_node && *arg2 == error_mark_node)
  2058.     error ("ambiguous pointer conversion");
  2059.     }
  2060.   if (*arg1 == 0)
  2061.     {
  2062.       if (*arg2 == 0 && type1 != type2)
  2063.     error ("default type conversion for types `%s' and `%s' failed",
  2064.            name1, name2);
  2065.       else
  2066.     error ("default type conversion for type `%s' failed", name1);
  2067.       return 0;
  2068.     }
  2069.   else if (*arg2 == 0)
  2070.     {
  2071.       error ("default type conversion for type `%s' failed", name2);
  2072.       return 0;
  2073.     }
  2074.   return 1;
  2075. }
  2076.  
  2077. /* Must convert two aggregate types to non-aggregate type.
  2078.    Attempts to find a non-ambiguous, "best" type conversion.
  2079.  
  2080.    Return 1 on success, 0 on failure.
  2081.  
  2082.    The type of the argument is expected to be of aggregate type here.
  2083.  
  2084.    @@ What are the real semantics of this supposed to be??? */
  2085. int
  2086. build_default_unary_type_conversion (code, arg)
  2087.      enum tree_code code;
  2088.      tree *arg;
  2089. {
  2090.   tree type = TREE_TYPE (*arg);
  2091.   tree id = TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
  2092.     ? TYPE_IDENTIFIER (type) : TYPE_NAME (type);
  2093.   char *name = IDENTIFIER_POINTER (id);
  2094.  
  2095.   if (! TYPE_HAS_CONVERSION (type))
  2096.     {
  2097.       error ("type conversion required for type `%s'", name);
  2098.       return 0;
  2099.     }
  2100.  
  2101.   if (TYPE_HAS_INT_CONVERSION (type) && TYPE_HAS_REAL_CONVERSION (type))
  2102.     warning ("ambiguous type conversion for type `%s', defaulting to int", name);
  2103.   if (TYPE_HAS_INT_CONVERSION (type))
  2104.     *arg = build_type_conversion (code, integer_type_node, *arg, 1);
  2105.   else if (TYPE_HAS_REAL_CONVERSION (type))
  2106.     *arg = build_type_conversion (code, double_type_node, *arg, 1);
  2107.   else
  2108.     {
  2109.       *arg = build_type_conversion (code, ptr_type_node, *arg, 1);
  2110.       if (*arg == error_mark_node)
  2111.     error ("ambiguous pointer conversion");
  2112.     }
  2113.   if (*arg == 0)
  2114.     {
  2115.       error ("default type conversion for type `%s' failed", name);
  2116.       return 0;
  2117.     }
  2118.   return 1;
  2119. }
  2120.